import { defineStore } from 'pinia'
import { ref } from 'vue'
export const useCounterStore = defineStore('cart', () => {

    let cart=ref([])
    function addCart(cartdata,goods_id){
        let username=localStorage.getItem('username')
        let usercart=cart.value.filter(item => item.username==username)
        let pdcart=usercart.find(item => item.goods.goods_id==goods_id)
        if(pdcart){
            pdcart.number++
        }else{
            cart.value.push({
                username:username,
                id:Math.floor(Math.random()*100000),
                checked:false,
                goods:cartdata,
                number:1
            })
        }
    }
    function delcart(id){
        cart.value=cart.value.filter(item => item.id !=id)
    }

    return {cart,addCart,delcart}
},
    {
        persist: true,
    },
)